package log;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.io.RandomAccessFile;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Log {

	public static void main(String[] args) {
		appendLog2("D:\\a.log", "叉叉叉");
	}
	
	
	public static void tempLog(String targetfilename, String content) {
		try {
			String path1=System.getenv("temp");
			String path2 = path1.replaceAll("\\\\", "/");
			if (!path2.endsWith("\\") && !path2.endsWith("/")) {
				path2 += "/";
			}
			SimpleDateFormat bartDateFormat = new SimpleDateFormat(
					"yyyy_MM_dd_HH_mm_ss");
			Date date = new Date();
			String datename = bartDateFormat.format(date);
			String fileName=path2+targetfilename+datename+".log";
			content = new String(content.getBytes("GBK"), "ISO-8859-1");
			RandomAccessFile randomFile = new RandomAccessFile(fileName, "rw");
			long fileLength = randomFile.length();
			randomFile.seek(fileLength);
			randomFile.writeBytes(content + "\r\n");
			randomFile.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static String getCurrentTime() {
		// 2012-9-10 11:50:18
		SimpleDateFormat bartDateFormat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		Date date = new Date();
		String currentTime = bartDateFormat.format(date);
		return currentTime+"    ";
	}
	
	public static void Log(String fileName, String content) {
		try {

			String path = fileName.replaceAll("\\\\", "/");
			if ((!path.endsWith(".txt")) && (!path.endsWith(".log"))) {
				SimpleDateFormat bartDateFormat = new SimpleDateFormat(
						"yyyy_MM_dd_HH_mm_ss");
				Date date = new Date();
				String log = bartDateFormat.format(date);
				path = path + log + ".txt";
			}
			File f = new File(path);
			String ParentsPath = f.getParent();
			System.out.println(ParentsPath);
			File ParentsFile = new File(ParentsPath);
			if (!ParentsPath.endsWith("\\") && !ParentsPath.endsWith("/")) {
				ParentsPath += "/";
			}
			if (!ParentsFile.exists()) {
				ParentsFile.mkdirs();
			}

			if (!f.exists()) {
				f.createNewFile();
			}
			content = new String(content.getBytes("GBK"), "ISO-8859-1");
			RandomAccessFile randomFile = new RandomAccessFile(path, "rw");
			long fileLength = randomFile.length();
			randomFile.seek(fileLength);
			randomFile.writeBytes(content + "\r\n");
			randomFile.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	/**
	 * @param fileName
	 * @param content
	 */
	public static void appendLog(String fileName, String content) {
		try {
			content = new String(content.getBytes("GBK"), "ISO-8859-1");
			RandomAccessFile randomFile = new RandomAccessFile(fileName, "rw");
			long fileLength = randomFile.length();
			randomFile.seek(fileLength);
			randomFile.writeBytes(content + "\r\n");
			randomFile.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static String getErrorInfo(Exception e) {
		String error = null;
		if (e != null) {
			try {
				ByteArrayOutputStream baos = new ByteArrayOutputStream();
				PrintStream ps = new PrintStream(baos);
				e.printStackTrace(ps);
				error = baos.toString();
				baos.close();
				ps.close();
			} catch (Exception e1) {
				error = e.toString();
			}
		}
		return error;
	}
	
	public static void appendLog2(String fileName, String content) {
		try {
			File f = new File(fileName);
			if(f.exists()){
				f.delete();
			}
			content = new String(content.getBytes("GBK"), "ISO-8859-1");
			RandomAccessFile randomFile = new RandomAccessFile(fileName, "rw");
			randomFile.writeBytes(content);
			randomFile.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
}
